﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Procedure = DeltaFlex.Data.ConsProcedures.Albara;

namespace DeltaFlex.Data
{
    public class Albara
    {
        public Albara()
        { }

        #region Inserts
        public static int InsertarAlbara(int idAlbara, int idOrdreTreball, int idClient, decimal importTotal)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = Procedure.SP_INSERTAR_ALBARA;
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDALBARA, idAlbara);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDORDRE_TREBALL, idOrdreTreball);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDCLIENT, idClient);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_TOTAL, importTotal);

            return Operacions_BD.Insertar(cmd);
        }
        #endregion

        #region Update
        public static void ActualitzarAlbara(int idAlbara, int idOrdre, string pedidoClient, string referenciaClient, string idTreball,
                                             string numTroquel, int idClient, DateTime data, decimal total)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = Procedure.SP_ACTUALITZAR_ALBARA;

            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDALBARA, idAlbara);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDORDRE_TREBALL, idOrdre);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_PEDIDOCLIENT,pedidoClient);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_REFERENCIACLIENT,referenciaClient);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDTREBALL,idTreball);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_NUMTROQUEL,numTroquel);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDCLIENT, idClient);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_DATA,data);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_TOTAL, total);

            Operacions_BD.Update(cmd);
        }

        #endregion

        #region Selects
        public static IDataReader Cercar_NextIdAlbara()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = Procedure.SP_CERCAR_NEXT_IDALBARA;
            cmd.CommandType = CommandType.StoredProcedure;

            return Operacions_BD.Reader(cmd);
        }

        public static IDataReader LlistarAlbarans(int idClient, int? idOrdre, DateTime? dataInici, DateTime? dataFi)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = Procedure.SP_CERCAR_ALBARANS;

            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDCLIENT, idClient);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDORDRE_TREBALL, idOrdre); 
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_DATAINICI, dataInici);
            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_DATAFI, dataFi);

            return Operacions_BD.Reader(cmd);
        }

        public static IDataReader LlistarAlbaraByIdAlbara(int idAlbara)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = Procedure.SP_CERCAR_ALBARANS_BYIDALBARA;

            cmd.Parameters.AddWithValue(Procedure.PARAMETRE_IDALBARA, idAlbara);

            return Operacions_BD.Reader(cmd);
        }
        #endregion

       
    }
}
